<template>
    <el-card>
        <template #header>
            <span>角色管理</span>
        </template>
        <el-button type="primary" @click="dialogVisible = true">新增</el-button>
        <el-table :data="roleData">
            <el-table-column label="ID" prop="id" align="center"></el-table-column>
            <el-table-column label="角色名称" prop="rolename" align="center"></el-table-column>
            <el-table-column label="角色状态" prop="state" align="center">
                <template #default="{ row }">
                    <el-switch v-model="row.state" :active-value="1" :inactive-value="0" active-text="ON" inactive-text="OFF" @change="handleChange($event,row.id)"/>
                </template>
            </el-table-column>
            <el-table-column label="操作" align="center" justify="middle">
                <template #default="{ row }">
                    <el-button type="primary" size="small" @click="rightManage(row.id,row.menu)">权限</el-button>
                    <el-button type="warning" size="small" @click="handleEdit(row)">编辑</el-button>
                    <el-button type="danger" size="small" @click="handleDelete(row.id)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <el-row align="middle" justify="center">
            <el-pagination background layout="prev, pager, next" :total="total" v-model:page-size="pageSize"
                v-model:current-page="page" @current-change="getData" />
        </el-row>
        <el-dialog v-model="dialogVisible" title="新增角色" width="500">
            <el-form label-width="80px" :model="addForm" :rules="rules" ref="formIns">
                <el-form-item label="角色名称" prop="rolename">
                    <el-input v-model="addForm.rolename"></el-input>
                </el-form-item>
            </el-form>
            <template #footer>
                <div class="dialog-footer">
                    <el-button @click="dialogVisible = false">取消</el-button>
                    <el-button type="primary" @click="handleSubmit">
                        提交
                    </el-button>
                </div>
            </template>
        </el-dialog>
        <el-dialog v-model="dialogVisible2" title="更新角色" width="500">
            <el-form label-width="80px" :model="editForm" :rules="rules" ref="formIns2">
                <el-form-item label="角色名称" prop="rolename">
                    <el-input v-model="editForm.rolename"></el-input>
                </el-form-item>
            </el-form>
            <template #footer>
                <div class="dialog-footer">
                    <el-button @click="dialogVisible2 = false">取消</el-button>
                    <el-button type="primary" @click="handleSubmit2">提交</el-button>
                </div>
            </template>
        </el-dialog>
        <el-dialog v-model="dialogVisible3" title="设置权限" width="500">
            <el-checkbox-group v-model="checkedMenu">
                <el-checkbox v-for="item in menuData" :key=item.id :value="item.label" :label="item.label"></el-checkbox>
            </el-checkbox-group>
            <template #footer>
                <div class="dialog-footer">
                    <el-button @click="dialogVisible3 = false">取消</el-button>
                    <el-button type="primary" @click="handleSubmit3">提交</el-button>
                </div>
            </template>
        </el-dialog>
    </el-card>
</template>
<script setup lang="ts">
import { ref, reactive, onMounted } from "vue";
import { getRole, addRole, findByRid, delRole,edtRole,getMenu } from "@/api/api";
import type { FormInstance, FormRules } from "element-plus"
import { ElMessage, ElMessageBox } from 'element-plus'
//查询
const roleData = ref<RoleData[]>([]);
const pageSize = ref(5);
const total = ref(0);
const page = ref(1);
const getData = async () => {
    const res = await getRole({
        page: page.value,
        limit: pageSize.value
    });
    roleData.value = res.data.data;
    total.value = res.data.total;
}
onMounted(() => {
    getData();
})
//新增
const dialogVisible = ref(false);
const addForm = reactive<RoleForm>({
    rolename: ""
})
const rules: FormRules = {
    rolename: { required: true, message: "请输入角色名", trigger: "blur" }
}
const formIns = ref<FormInstance>();
const handleSubmit = () => {
    formIns.value?.validate(async (v) => {
        if (v) {
            const res = await addRole(addForm);
            if (res.data.code === 200) {
                ElMessage.success("新增成功");
                dialogVisible.value = false;
                formIns.value?.resetFields();
                getData();
            } else {
                ElMessage.error(res.data.msg);
            }
        }
    })
}
//删除
const handleDelete = async (id: number) => {
    const res = await findByRid(id);
    if (res.data.code === 201) {
        ElMessage.error("当前角色下有用户，不能进行删除操作");
        return;
    }
    try {
        await ElMessageBox.confirm(
            '确认删除当前角色吗?确认继续',
            '警告',
            {
                confirmButtonText: '确认',
                cancelButtonText: '取消',
                type: 'warning',
            }
        );
        const res = await delRole(id);
        if (res.data.code === 200) {
            ElMessage.success("删除成功");
            getData();
        } else {
            ElMessage.error("删除失败");
        }
    } catch (e) {
        ElMessage.info("取消删除")
    }

}
//编辑
const handleChange=async(val:number,id:number)=>{
    const res=await edtRole(id,{state:val});
    if(res.data.code===200){
        ElMessage.success("状态更新成功")
    }
}
const dialogVisible2=ref(false);
const editForm=reactive<RoleForm>({
    rolename:""
});
const formIns2=ref<FormInstance>();
let editid:number;
const handleEdit=(body:RoleData)=>{
    dialogVisible2.value=true;
    editForm.rolename=body.rolename;
    editid=body.id;
}
const handleSubmit2=async()=>{
    dialogVisible2.value=false;
    const res=await edtRole(editid,editForm);
    if(res.data.code===200){
        ElMessage.success("更新角色名成功");
        getData();
    }else{
        ElMessage.error(res.data.msg);
    }
}
//编辑权限
const menuData=ref<MenuData[]>([]);
const checkedMenu=ref<string[]>([]);
onMounted(async ()=>{
    const res=await getMenu({page:1,limit:20});
    menuData.value=res.data.data;
})
const dialogVisible3=ref(false);
const rightManage=(id:number,menu:string)=>{
    dialogVisible3.value=true;
    editid=id;
    checkedMenu.value=JSON.parse(menu);
}
const handleSubmit3=async()=>{
    dialogVisible3.value=false;
    const res=await edtRole(editid,{menu:JSON.stringify(checkedMenu.value)});
    if(res.data.code===200){
        ElMessage.success("更新权限成功");
        getData();
    }else{
        ElMessage.error(res.data.msg);
    }
}
</script>
<style scoped>
.el-row {
    height: 60px;
}
</style>